package admin

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/juju/errors"
	"net/http"
	"qihuang/config"
	"qihuang/db/redis"
	"qihuang/loger"
	"qihuang/protocol"
	"qihuang/service/admin"
	"qihuang/validate"
	"strconv"
	"time"
)

type Articles struct {
}

type AddParams struct {
	CategoryId      int    `json:"category_id"  validate:"gt=0"`
	Title           string `json:"title"  validate:"gt=4"`
	Summary         string `json:"summary"`
	Content         string `json:"content" validate:"required"`
	Keywords        string `json:"keywords"`
	ImgPath         string `json:"img_path"`
	OriginalAddress string `json:"Original_address"`
	CreateTime      string `json:"create_time"`
	ModifyTime      string `json:"modify_time"`
	Status          int    `json:"status" validate:"required"`
}

//添加文章
func (this *Articles) Add(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	var addParams AddParams
	err := c.ShouldBindJSON(&addParams)
	jsonstr, _ := json.Marshal(addParams)
	loger.Loger.Info("Articles-Add-Params:", string(jsonstr))
	if err != nil {
		loger.Loger.Info(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}

	validator, _ := validate.Default()
	if check := validator.CheckStruct(addParams); !check {
		resp.Status = 1
		resp.Msg = validator.GetOneError()
		c.JSON(http.StatusOK, resp)
		return
	}

	//if !helper.IsTimeStr(addParams.PublishTime) {
	//	resp.Status = -1
	//	resp.Msg = "发布时间格式错误"
	//	c.JSON(http.StatusOK, resp)
	//	return
	//}
	//if addParams.ShowType <= 0 {
	//	addParams.ShowType = 2
	//}
	params := &admin.ArticleParams{}
	params.CategoryId = addParams.CategoryId
	params.Title = addParams.Title
	params.Summary = addParams.Summary
	params.Keywords = addParams.Keywords
	params.Content = addParams.Content
	params.ImgPath = addParams.ImgPath
	params.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	params.OriginalAddress = addParams.OriginalAddress
	params.Status = addParams.Status

	articleServ := admin.Articles{}
	resp = articleServ.Add(params)
	c.JSON(http.StatusOK, resp)
}

//文章列表
func (this *Articles) GetList(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "2", Data: ""}
	page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err != nil {
		page = 1
	}
	pageSize, err := strconv.Atoi(c.DefaultQuery("page_size", "10"))
	if err != nil {
		pageSize = 10
	}
	cateId, err := strconv.Atoi(c.DefaultQuery("category_id", "0"))
	if err != nil {
		cateId = 0
	}
	articleServ := admin.Articles{}
	resp = articleServ.GetList(page, pageSize, cateId, []string{"id", "category_id", "title", "summary", "modify_time", "status"})
	c.JSON(http.StatusOK, resp)
}

//删除文章
func (this *Articles) Delete(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	articleId, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		loger.Loger.Info(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	articleServ := admin.Articles{}
	resp = articleServ.Delete(articleId)
	c.JSON(http.StatusOK, resp)
}

//更新文章
func (this *Articles) Update(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	articleId, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		loger.Loger.Info(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	var addParams AddParams
	err = c.ShouldBindJSON(&addParams)
	jsonstr, _ := json.Marshal(addParams)
	loger.Loger.Info("Articles-Update-Params:", string(jsonstr))
	if err != nil {
		loger.Loger.Info(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}

	validator, _ := validate.Default()
	if check := validator.CheckStruct(addParams); !check {
		resp.Status = 1
		resp.Msg = validator.GetOneError()
		c.JSON(http.StatusOK, resp)
		return
	}

	//if !helper.IsTimeStr(addParams.PublishTime) {
	//	resp.Status = -1
	//	resp.Msg = "发布时间格式错误"
	//	c.JSON(http.StatusOK, resp)
	//	return
	//}
	//if addParams.ShowType <= 0 {
	//	addParams.ShowType = 2
	//}
	params := &admin.ArticleParams{}
	params.CategoryId = addParams.CategoryId
	params.Title = addParams.Title
	params.Summary = addParams.Summary
	params.Keywords = addParams.Keywords
	params.Content = addParams.Content
	params.ImgPath = addParams.ImgPath
	params.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
	params.OriginalAddress = addParams.OriginalAddress
	params.Status = addParams.Status
	//admin_id, _ := c.Get("admin_id")
	//username, _ := c.Get("username")
	//aop_id, _ := strconv.Atoi(admin_id.(string))
	//params.OpId = aop_id
	//params.OpUser = username.(string)

	articleServ := admin.Articles{}
	resp = articleServ.Update(articleId, params)
	c.JSON(http.StatusOK, resp)
}

func (this *Articles) Show(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	articleId, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		loger.Loger.Error(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	articleServ := admin.Articles{}
	resp = articleServ.Detail(articleId)
	c.JSON(http.StatusOK, resp)
}

func (this *Articles) DeleteCache(c *gin.Context) {
	resp := &protocol.Resp{Status: 1, Msg: "", Data: ""}
	cacheKey := "article_cates_" + config.Configs.RedisCacheVersion
	redisConn := redis.RedisClient.Pool.Get()
	if err := redisConn.Err(); err != nil {
		loger.Loger.Error(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "系统错误"
		return
	}
	_, err := redisConn.Do("del", cacheKey)
	if err != nil {
		loger.Loger.Error(errors.ErrorStack(errors.Trace(err)))
		resp.Status = 1
		resp.Msg = "系统错误"
		return
	}
	resp.Status = 0
	c.JSON(http.StatusOK, resp)
}
